package com.wuming.house.controller;

import com.wuming.house.core.CommonResult;
import com.wuming.house.core.DecorateImageType;
import com.wuming.house.core.HouseType;
import com.wuming.house.core.JsonResult;
import com.wuming.house.model.*;
import com.wuming.house.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author xuhuai
 * @createTime 2019/9/5 16:40
 * @Description:
 */
@Controller
@RequestMapping("/web/decorateImg")
public class DecorateImgController extends BaseController {

    @Autowired
    private DecorateImgService decorateImgService;

    @Autowired
    private GoodImageService goodImageService;

    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam Integer decorateId,@RequestParam Integer picType,
                             @RequestParam(defaultValue = "1") int pageIndex,
                             @RequestParam(defaultValue = "10") int pageSize) {
        logger.info("准备查询楼盘id:{}的图片信息", decorateId);
        try {
            List<DecorateImg> decorateImgs = decorateImgService.getDecorateImgByDecorateId(decorateId, picType, pageIndex, pageSize);
            int total = decorateImgService.getTotal(decorateId,picType);
            return CommonResult.createSuccessResult(decorateImgs, total, "查询案例详情图片信息成功");
        } catch (Exception e) {
            logger.error("查询案例详情图片信息失败,失败信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询案例详情图片信息失败");
        }
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public JsonResult delete(@PathVariable Long id) {
        logger.info("准备删除id为:{}的案例详情图片", id);
        try {
            decorateImgService.deleteById(id);
            return new JsonResult(null, "删除案例详情图片成功", true);
        } catch (Exception e) {
            logger.error("删除案例详情图片失败,失败信息:{}", e.getMessage());
            return new JsonResult(null, "删除案例详情图片失败", false);
        }
    }

    @ResponseBody
    @RequestMapping("/uploadImg/{picType}")
    public JsonResult uploadImg(@PathVariable Integer picType, MultipartFile file, @RequestParam Integer decorateId) {
        try {
            String path = this.uploadImage(file, "decorateDetail", 1);
            Date date = new Date();
            GoodImage goodImage = new GoodImage();
            goodImage.setImagePath(path);
            goodImage.setCreateDatetime(date);
            goodImageService.saveImage(goodImage);
            DecorateImg decorateImg = new DecorateImg();
            decorateImg.setPicId(goodImage.getId().longValue());
            decorateImg.setPicUrl(path);
            decorateImg.setDecorateId(decorateId);
            decorateImg.setPicType(picType);
            decorateImg.setCreateDatetime(date);
            decorateImgService.saveDecorateImg(decorateImg);
            return new JsonResult(null, "上传成功", true);
        } catch (IOException ie) {
            ie.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new JsonResult("", "上传失败", false);
    }

    @RequestMapping(value = "/manageImg/{picType}/{decorateId}",method = RequestMethod.GET)
    public String manageImg(@PathVariable Integer picType,@PathVariable Integer decorateId, Model model){
        model.addAttribute("decorateId",decorateId);
        if (picType==DecorateImageType.TOP_DETAIL.getValue()) {
            return "/tools/manageDecorateDetail";
        }else {
            return "/tools/manageDecorateFootDetail";
        }
    }
}
